#!/bin/bash

DIST_NAME="qTeXPre-`cat VERSION`-src"
OUT_FILENAME="${DIST_NAME}.tar.gz"

rm -rf __dist
mkdir -p __dist/$DIST_NAME

echo -n '[SVN listing]: '
if svn status -v > __dist/files; then
	FILES=`cat __dist/files | grep -v '^\? ' | awk '{print $NF;}'`
fi
if [ -z "$FILES" ]; then
	echo 'Can`t find version controlled files...'
	echo 'Failed.'
	exit 1
fi
echo 'OK'

echo -n '[Copying files]: '
for COPY_FILE in $FILES; do
	if [ ! -d $COPY_FILE ]; then
		td="__dist/$DIST_NAME/`dirname $COPY_FILE`"
		if [ ! -d $td ]; then 
			mkdir -p $td
		fi
		cp -p $COPY_FILE $td
	fi
done
echo 'OK'

echo -n '[TAR]: '
cd __dist
tar cvfz ../$OUT_FILENAME $DIST_NAME > /dev/null
cd ..
echo 'OK'

echo -n '[Cleanup]: '
rm -rf __dist
echo 'OK'

echo "Done: ${DIST_NAME}.tar.gz"
